package com.pizza.spring.rmi;

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.Serializable;

import javax.imageio.ImageIO;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.swing.ImageIcon;



@Entity
@Table(name="PRODUCT")
public class Produkt implements Serializable{
	@Id
	@GeneratedValue
	long id;
	@Column(name="name")
	private String name;
	
	@Column(name="description")
	private String descriptions;
	
	@Transient
	ImageIcon imageIcon;

	@Column(name="avatar" ,columnDefinition = "LONGBLOB")
	private byte[] photo;
	
	public Produkt() {
	}
	public Produkt(String name, String descriptions, ImageIcon imageIcon) {
		super();
		this.name = name;
		this.descriptions = descriptions;
		this.imageIcon=imageIcon;
		this.photo=getImgBytes(imageIcon.getImage());
	}
	private byte [] getImgBytes(Image image) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {
            ImageIO.write(getBufferedImage(image), "JPEG", baos);
        } catch (IOException ex) {
            //handle it here.... not implemented yet...
        }
        return baos.toByteArray();
    }
	  
    private BufferedImage getBufferedImage(Image image) {
        int width = image.getWidth(null);
        int height = image.getHeight(null);
        BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        //Graphics2D g2d = bi.createGraphics();
        //g2d.drawImage(image, 0, 0, null);
        return bi;
    }
	public long getId() {
		return id;
	}
	public void setId(long id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getDescriptions() {
		return descriptions;
	}
	public void setDescriptions(String descriptions) {
		this.descriptions = descriptions;
	}
	public ImageIcon getImageIcon() {
		return new ImageIcon("C:/Temp/icon.jpg");
	}
	public void setImageIcon(ImageIcon imageIcon) {
		this.imageIcon = imageIcon;
		photo=getImgBytes(imageIcon.getImage());
	}
}
